package com.fanghuyun.iswaf.function;

import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.fanghuyun.iswaf.common.ClientLogger;
import com.fanghuyun.iswaf.common.Constants;
import com.fanghuyun.iswaf.common.SysConfig;
import com.fanghuyun.iswaf.filter.DoFilter;
import com.fanghuyun.iswaf.util.StringUtil;

public class Struts extends DoFilter{
	
	private static Logger logger = Logger.getLogger("iswaf");
	
	public static boolean pocCheck(HttpServletRequest request, Map<String, Object> wrapper,Properties properties){
		try {
			SysConfig config = getSysConfig();
			Map<String,Object> requestMap = config.getRequestParameter(request);
			String regex = properties.getProperty("function.struts.poc").trim();
			if(StringUtil.isNotEmpty(regex)){
				for(String b:requestMap.keySet()){
					boolean key = Pattern.compile(regex,Pattern.DOTALL|Pattern.CASE_INSENSITIVE).matcher(b).find();
					boolean value = Pattern.compile(regex,Pattern.DOTALL|Pattern.CASE_INSENSITIVE).matcher((CharSequence) requestMap.get(b)).find();
					boolean query = StringUtil.isNotEmpty(request.getQueryString())?Pattern.compile(regex,Pattern.DOTALL|Pattern.CASE_INSENSITIVE).matcher(request.getQueryString()).find():false;
					if(key||value||query){
						ClientLogger.addAttackLogs(request, Constants.SYS_FUNCTION_STRUTS_RULES);
						return true;
					}
				}
			}else{
				logger.info("Struts2模块未加载...");
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return false;
	}
	
}
